import requests
import json
from config.settings import VT_KEY

class VirustotalScan:
    """
    Get Subdomains by Virustotal
    """
    name = 'Virustotal'
    
    def __init__(self):
        self.query = ''
        self.register(VT_KEY)
        self.results = set()
    
    @property
    def fields(self):
        return ['subdomains']

    def scan(self, query: str):
        self.query = query
        self.run()
        
    def register(self, key):
        self.key = key
        self.base_url = "https://www.virustotal.com"
        self.search_api_url = "/vtapi/v2/domain/report"
        
        
    def get_domain_infos(self):
        api_full_url = "{base}{query}".format(
            base=self.base_url, query=self.search_api_url)
        params = {"apikey": self.key, "domain": self.query}
        res = requests.get(api_full_url, params).json()
        try:
            [self.results.add(domain) for domain in res['subdomains'] ]
        except Exception as e:
            print(e)

    def run(self):
        self.get_domain_infos()
